<?php session_start(); ?>
<?php include("php/sessionValidate.php"); ?> 
<html>

<head>
<title>Privr - Private Messaging for Everyone</title>
<link href="main.css" rel="stylesheet" type="text/css">
<script type="text/javascript">

	function initialLoad() {
		ajaxShowMessages();
		ajaxShowGroupMembership();
		ajaxShowInvites();
		ajaxShowOtherMembers();
		ajaxShowMyInvites();
	}

	function createGroup() {
		document.getElementById('newGroup').innerHTML = 
			"<table>\n\r"
			+ "<tr><td>group name:</td> <td><input type='text' id='groupName' name='groupName' /></td></tr>\n\r"
			+ "<tr><td>group key:</td> <td><input type='text' id='groupPublicKey' name='groupPublicKey' /></td></tr>\n\r"
			+ "<tr><td>group description:</td> <td><input type='text' id='groupDescription' name='groupDescription' /></td></tr>\n\r"
			+ "</table>"
			+ "<button onclick='ajaxCreateGroup()' >Create Group</button>";			
	}
	
	function sendGroupMessage() {
		document.getElementById('sendMessage').innerHTML = 
			"<table>\n\r"
			+ "<tr><td>group name:</td> <td><input type='text' id='groupName' name='groupName' /></td></tr>\n\r"
			+ "<tr><td>user name:</td> <td><input type='text' id='userName' name='userName' /></td></tr>\n\r"
			+ "<tr><td>message:</td> <td><input type='text' id='message' name='message' /></td></tr>\n\r"
			+ "<tr><td>retention mode:</td> <td><input type='text' id='retentionMode' name='retentionMode' /></td></tr>\n\r"
			+ "<tr><td>expiration date:</td> <td><input type='text' id='expirationDate' name='expirationDate' /></td></tr>\n\r"
			+ "</table>"
			+ "<button onclick='ajaxSendGroupMessage()' >Send Message</button>";
	}
	
	function ajaxSendGroupMessage() {
		var groupName = document.getElementById('groupName').value;
		var userName = document.getElementById('userName').value;
		var message = document.getElementById('message').value;
		var retentionMode = document.getElementById('retentionMode').value;
		var expirationDate = document.getElementById('expirationDate').value;
		
		//TODO: validate
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpGrpDel=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpGrpDel=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpGrpDel.onreadystatechange=function()
		{
			if (xmlhttpGrpDel.readyState==4 && xmlhttpGrpDel.status==200)
			{
				document.getElementById("sendMessage2").innerHTML=xmlhttpGrpDel.responseText;
			}	
		}
		xmlhttpGrpDel.open("GET","sendGroupMessage.php?groupName=" + groupName + "&userName=" + userName + "&message=" + message+ "&retentionMode=" + retentionMode+ "&expirationDate=" + expirationDate ,true);
		xmlhttpGrpDel.send();
	}
	
	function cancelInvite() {
		if (document.getElementById("cancelinviteID").value == "") {
			document.getElementById("invitecancelerr").innerHTML = "Please choose an invite to delete.";
		} else {
			document.getElementById("invitecancelerr").innerHTML = ""
			ajaxCancelInviteID();
		}
	}
	
	function acceptInvite() {
		if (document.getElementById("myinv").value == "") {
			document.getElementById("myinviteserr").innerHTML = "Please choose an invite to accept.";
		} else {
			document.getElementById("myinviteserr").innerHTML = "";
			ajaxAcceptInviteID();
		}
	}
	
	function declineInvite() {
		if (document.getElementById("myinv").value == "") {
			document.getElementById("myinviteserr").innerHTML = "Please choose an invite to decline.";
		} else {
			document.getElementById("myinviteserr").innerHTML = "";
			ajaxDeclineInviteID();
		}
	}

	function showDeleteGroup() {
		if (document.getElementById("deleteGroup").innerHTML == "") {
			document.getElementById("deleteGroup2").innerHTML = ""
			ajaxShowDeleteGroup();
		} else {
			document.getElementById("deleteGroup2").innerHTML = "";
			document.getElementById("deleteGroup").innerHTML = "";
		}
	}

	
	function deleteGroup() {
		if (document.getElementById("selGroup").value != "") {
			ajaxDeleteGroup();
		} else {
			document.getElementById("deleteGroup2").innerHTML = "Please select a group you'd like to delete.";
		}
	}
	
	function leaveGroup() {
		if (document.getElementById("leaveGroup").value != "") {
			ajaxLeaveGroup();
		} else {
			document.getElementById("leaveGroup").innerHTML = "Please select a group you'd like to delete.";
		}
	}
	
	function showInviteUser() {
		if (document.getElementById("inviteUserForm").innerHTML == "") {
			ajaxShowInviteUserForm();
		} else {
			document.getElementById("inviteUserForm").innerHTML = "";
		}
	}
	


	// AJAX Functions
	xmlhttp=new XMLHttpRequest();
	
	function ajaxCreateGroup() {
		var groupName = document.getElementById('groupName').value;
		var groupPublicKey = document.getElementById('groupPublicKey').value;
		var groupDescription = document.getElementById('groupDescription').value;

		
		//if (str.length==0)
		//{
		//	document.getElementById("newGroup2").innerHTML="";
		//	return;
		//}
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				document.getElementById("newGroup2").innerHTML=xmlhttp.responseText;
				initialLoad();
			}	
		}
		xmlhttp.open("GET","createGroup.php?groupName=" +groupName+ "&groupPublicKey=" +groupPublicKey+ "&groupDescription=" +groupDescription,true);
		xmlhttp.send();
	}
		
	function ajaxShowDeleteGroup() {
		var username = document.getElementById('username').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpGrpDel=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpGrpDel=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpGrpDel.onreadystatechange=function()
		{
			if (xmlhttpGrpDel.readyState==4 && xmlhttpGrpDel.status==200)
			{
				document.getElementById("deleteGroup").innerHTML=xmlhttpGrpDel.responseText;
			}	
		}
		xmlhttpGrpDel.open("GET","php/results/returndelgroupsforuser.php?username=" + username,true);
		xmlhttpGrpDel.send();
	}
	
	
	
	function ajaxShowGroupMembership() {
		var username = document.getElementById('username').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpGrp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpGrp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpGrp.onreadystatechange=function()
		{
			if (xmlhttpGrp.readyState==4 && xmlhttpGrp.status==200)
			{
				document.getElementById("groupMembership").innerHTML=xmlhttpGrp.responseText;
			}	
		}
		xmlhttpGrp.open("GET","php/results/returngroupsforuser.php?username=" + username,true);
		xmlhttpGrp.send();
	}
	
	
	function ajaxShowOtherMembers() {
		var username = document.getElementById('username').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpOthMem=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpOthMem=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpOthMem.onreadystatechange=function()
		{
			if (xmlhttpOthMem.readyState==4 && xmlhttpOthMem.status==200)
			{
				document.getElementById("otherMembers").innerHTML=xmlhttpOthMem.responseText;
			}	
		}
		xmlhttpOthMem.open("GET","php/results/returnothermembers.php?username=" + username,true);
		xmlhttpOthMem.send();
	}
	
	
	function ajaxShowInvites() {
		var username = document.getElementById('username').value;
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInv.onreadystatechange=function()
		{
			if (xmlhttpInv.readyState==4 && xmlhttpInv.status==200)
			{
				document.getElementById("invites").innerHTML=xmlhttpInv.responseText;
			}	
		}
		xmlhttpInv.open("GET","php/results/returninvitesforgroup.php?username=" + username,true);
		xmlhttpInv.send();
	}
	
	function ajaxShowInviteUserForm() {
		var username = document.getElementById('username').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInvForm=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInvForm=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInvForm.onreadystatechange=function()
		{
			if (xmlhttpInvForm.readyState==4 && xmlhttpInvForm.status==200)
			{
				document.getElementById("inviteUserForm").innerHTML=xmlhttpInvForm.responseText;
			}	
		}
		xmlhttpInvForm.open("GET","php/inviteuserform.php?username=" + username,true);
		xmlhttpInvForm.send();
	}
	
	
	function ajaxShowMessages() {
		var username = document.getElementById('username').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpMsg=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpMsg=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpMsg.onreadystatechange=function()
		{
			if (xmlhttpMsg.readyState==4 && xmlhttpMsg.status==200)
			{
				document.getElementById("messages").innerHTML=xmlhttpMsg.responseText;
			}	
		}
		xmlhttpMsg.open("GET","php/results/returnmsgforuser.php?username=" + username,true);
		xmlhttpMsg.send();
	}
	
	
	function ajaxDeleteGroup() {
		var groupname = document.getElementById('selGroup').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpDel=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpDel=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpDel.onreadystatechange=function()
		{
			if (xmlhttpDel.readyState==4 && xmlhttpDel.status==200)
			{
				document.getElementById("deleteGroup").innerHTML="";
				document.getElementById("deleteGroup2").innerHTML=xmlhttpDel.responseText;
				initialLoad();
			}	
		}
		xmlhttpDel.open("GET","php/results/cascadedeletegroup.php?groupname=" + groupname,true);
		xmlhttpDel.send();
	}
	
	function ajaxLeaveGroup() {
		var username = document.getElementById('username').value;
		var groupname = document.getElementById('leaveGroup').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpLeave=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpLeave=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpLeave.onreadystatechange=function()
		{
			if (xmlhttpLeave.readyState==4 && xmlhttpLeave.status==200)
			{
				document.getElementById("leaveGroup").innerHTML=xmlhttpLeave.responseText;
				initialLoad();
			}	
		}
		xmlhttpLeave.open("GET","php/results/returnleavegroup.php?username=" + username + "&groupname=" + groupname,true);
		xmlhttpLeave.send();
	}
	
	
	function ajaxInviteUser() {
		var username = document.getElementById('username').value;
		var inviteUser = document.getElementById('inviteUser').value;
		var selinviteGroup = document.getElementById('selinviteGroup').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInvActn=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInvActn=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInvActn.onreadystatechange=function()
		{
			if (xmlhttpInvActn.readyState==4 && xmlhttpInvActn.status==200)
			{
				document.getElementById("inviteUserForm").innerHTML=xmlhttpInvActn.responseText;
				ajaxShowInvites();
			}	
		}
		xmlhttpInvActn.open("GET","php/results/returninviteuser.php?username=" + username + "&inviteUser=" + inviteUser + "&selinviteGroup=" + selinviteGroup,true);
		xmlhttpInvActn.send();
	}
	
	
	function ajaxCancelInviteID() {
		var cancelinviteID = document.getElementById('cancelinviteID').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpInvCancel=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpInvCancel=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpInvCancel.onreadystatechange=function()
		{
			if (xmlhttpInvCancel.readyState==4 && xmlhttpInvCancel.status==200)
			{
				document.getElementById("invitecancelerr").innerHTML=xmlhttpInvCancel.responseText;
				ajaxShowInvites();
			}	
		}
		xmlhttpInvCancel.open("GET","php/results/returncancelinvite.php?cancelinviteID=" + cancelinviteID,true);
		xmlhttpInvCancel.send();
	}
	
	function ajaxShowMyInvites() {
		var username = document.getElementById('username').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpMyInv=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpMyInv=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpMyInv.onreadystatechange=function()
		{
			if (xmlhttpMyInv.readyState==4 && xmlhttpMyInv.status==200)
			{
				document.getElementById("myinvites").innerHTML=xmlhttpMyInv.responseText;
			}	
		}
		xmlhttpMyInv.open("GET","php/results/returnmyinvites.php?username=" + username,true);
		xmlhttpMyInv.send();
	}

	function ajaxAcceptInviteID() {
		var myinvid = document.getElementById('myinv').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpMyInvAccept=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpMyInvAccept=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpMyInvAccept.onreadystatechange=function()
		{
			if (xmlhttpMyInvAccept.readyState==4 && xmlhttpMyInvAccept.status==200)
			{
				document.getElementById("myinviteserr").innerHTML=xmlhttpMyInvAccept.responseText;
				initialLoad();
			}	
		}
		xmlhttpMyInvAccept.open("GET","php/results/acceptinvite.php?myinv=" + myinvid,true);
		xmlhttpMyInvAccept.send();
	}	
	
	
	function ajaxDeclineInviteID() {
		var myinvid = document.getElementById('myinv').value;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpMyInvDecline=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttpMyInvDecline=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpMyInvDecline.onreadystatechange=function()
		{
			if (xmlhttpMyInvDecline.readyState==4 && xmlhttpMyInvDecline.status==200)
			{
				document.getElementById("myinviteserr").innerHTML=xmlhttpMyInvDecline.responseText;
				initialLoad();
			}	
		}
		xmlhttpMyInvDecline.open("GET","php/results/declineinvite.php?myinv=" + myinvid,true);
		xmlhttpMyInvDecline.send();
	}	
	
</script>
</head>

<body onload="initialLoad()">
Session Validated<br/>
<div id=container name=container>
<p>
<?php
echo "User ". $_SESSION['username'] .", welcome.";
?>
</p>
<?php 
echo "<input type='hidden' id='username' value='" . $_SESSION['username'] . "'>";
echo "<input type='hidden' id='groupname' value='" . $_SESSION['groupname'] . "'>";
?>

<?php include("includes/navigationbar.html"); ?> 
<p>---</p>

<div id="newGroup"></div>
<div id="newGroup2"></div>
<div id="deleteGroup"></div>
<div id="deleteGroup2"></div>
<div id="sendMessage"></div>
<div id="sendMessage2"></div>


<?php
// messages
?>
Messages from your groups:<br>
<div id="messages"></div>
<div id="newMessageForm"></div>
<p>---</p>
<?php
// members
?>
Other members in your groups:<br>
Remember, members in one group cannot see members in other groups unless they are part of that group too!<br>
<div id="otherMembers"></div>
<p>---</p>
<?php
// invites
?>
Your group's outgoing invites:<br>
<div id="invites"></div>
<div id="inviteUserForm"></div>
<div id="invitecancelerr"></div>
<br>
Incoming group invites:<br>
<div id="myinvites"></div>
<div id="myinviteserr"></div>
<p>---</p>
<?php
// groups
?>
You are part of the following groups:<br>
<div id="groupMembership"></div>
<div id="leaveGroup"></div>
</div>
</body>
</html>